package com.gwf.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.gwf.es.client.ElasticsearchService;
import com.gwf.pojo.User;
import com.gwf.service.UserService;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Title: UserServiceImpl
 * Description:
 * 用户操作实现类
 * Version:1.0.0
 *
 * @author pancm
 * @date 2018年1月9日
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    ElasticsearchService elasticsearchService;

    @Override
    public boolean insert(User user) {
        boolean falg = false;
        try {
            elasticsearchService.addData(
                    (JSONObject) JSONObject.toJSON(user),
                    "es-index",
                    "user-type",
                    user.getId().toString());
            falg = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return falg;
    }

    @Override
    public List<User> search(String searchContent) {

		BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();

		//filter 效率比 must高的多
		//if (StringUtils.isNotBlank(query.getRouterDatabaseNo())) {
		//	queryBuilder.filter(QueryBuilders.termQuery("routerDatabaseNo", query.getRouterDatabaseNo()));
		//}
		//
		//if (StringUtils.isNotBlank(query.getTradeNo())) {
		//	queryBuilder.filter(QueryBuilders.termQuery("tradeNo", query.getTradeNo()));
		//}
		//
		//if (null != query.getSystemId()) {
		//	queryBuilder.filter(QueryBuilders.termQuery("systemId", query.getSystemId()));
		//}
		//
		//if (null != query.getRefundType()) {
		//	queryBuilder.filter(QueryBuilders.termQuery("refundType", query.getRefundType()));
		//}
		//
		//if (null != query.getRefundStatus()) {
		//	queryBuilder.filter(QueryBuilders.termQuery("refundStatus", query.getRefundStatus()));
		//}
		//
		////时间段 一定要有头有尾 不然会出现慢查询
		//if (null != query.getCreateTime() && null != query.getUpdateTime()) {
		//	queryBuilder.filter(QueryBuilders.rangeQuery("createTime").from( query.getCreateTime()).to(query.getUpdateTime()));
		//}

        List<User> list = elasticsearchService.searchListData(
                "es-index",
                "user-type",
                User.class,
                "name,age",
				queryBuilder);
        return list;
    }


    @Override
    public List<User> searchUser(Integer pageNumber, Integer pageSize, String searchContent) {
        // 分页参数
        //Pageable pageable = new PageRequest(pageNumber, pageSize);
        //QueryStringQueryBuilder builder = new QueryStringQueryBuilder(searchContent);
        //SearchQuery searchQuery = new NativeSearchQueryBuilder().withPageable(pageable).withQuery(builder).build();
        //System.out.println("查询的语句:" + searchQuery.getQuery().toString());
        //Page<User> searchPageResults = userDao.search(searchQuery);
        //return searchPageResults.getContent();

        return null;
    }


    @Override
    public List<User> searchUserByWeight(String searchContent) {
        // 根据权重进行查询
        //FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery()
        //        .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("name", searchContent)),
        //                ScoreFunctionBuilders.weightFactorFunction(10))
        //        .add(QueryBuilders.boolQuery().should(QueryBuilders.matchQuery("description", searchContent)),
        //                ScoreFunctionBuilders.weightFactorFunction(100)).setMinScore(2);
        //System.out.println("查询的语句:" + functionScoreQueryBuilder.toString());
        //Iterable<User> searchResult = userDao.search(functionScoreQueryBuilder);
        //Iterator<User> iterator = searchResult.iterator();
        //List<User> list = new ArrayList<User>();
        //while (iterator.hasNext()) {
        //    list.add(iterator.next());
        //}
        //return list;

        return null;
    }


}
